package com.origin.chaos.player.controller;

import com.origin.chaos.common.bean.vo.Result;
import com.origin.chaos.framework.bean.vo.CanvasInfoVo;
import com.origin.chaos.framework.core.service.ICanvasService;
import com.origin.chaos.player.bean.vo.UpdatePixelVo;
import com.origin.chaos.security.bean.vo.ChangeInfoVo;
import com.origin.chaos.security.bean.vo.UserInfoVo;
import com.origin.chaos.security.serivce.IUserService;
import com.origin.chaos.security.util.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: tao.ding
 * @date 2021-07-16
 */
@RestController
@RequestMapping("/api/play")
@Api(tags = "玩家接口模块")
@Slf4j
public class PlayController {

    private ICanvasService canvasService;

    private IUserService userService;

    @GetMapping("/canvases")
    @ApiOperation("查询所有画布")
    public Result<List<CanvasInfoVo>> getAllCanvas() {
        return Result.ofBody(this.canvasService.listCanvases());
    }

    @PutMapping("/canvas/pixel")
    @ApiOperation("更新像素位")
    public Result updatePixel(@RequestBody UpdatePixelVo vo) {
        this.canvasService.updatePixel(vo);
        return Result.defaultSuccess();
    }

    @GetMapping("/canvas/{canvasId}/test")
    @ApiOperation("获取整个画布的数组-测试用")
    public Result<String[][]> getCanvasPixelTest(@ApiParam(value = "画布ID", required = true, example = "1")
                                                 @PathVariable("canvasId") Long canvasId) {
        TokenUtil.ensureLogin();
        long s = System.currentTimeMillis();
        Result<String[][]> r = Result.ofBody(this.canvasService.getCanvas(canvasId).getRgbHexStringArray());
        System.out.println(System.currentTimeMillis() - s);
        return r;
    }

    @GetMapping("/canvas/{canvasId}")
    @ApiOperation("获取整个画布的图像")
    public Result<byte[]> getCanvasPixel(@ApiParam(value = "画布ID", required = true, example = "1")
                                         @PathVariable("canvasId") Long canvasId) {
        TokenUtil.ensureLogin();
        long s = System.currentTimeMillis();
        byte[] r = this.canvasService.getCanvas(canvasId).getCanvasImageByteArray();
        System.out.println(System.currentTimeMillis() - s);
        return Result.ofBody(r);
    }

    @GetMapping("/user-info")
    @ApiOperation("获取当前登录用户信息")
    public Result<UserInfoVo> getUserInfo() {
        return Result.ofBody(this.userService.getUserInfo());
    }

    @PostMapping("/logout")
    @ApiOperation("退出登录")
    public Result logout() {
        this.userService.logout();
        return Result.defaultSuccess();
    }

    @PostMapping("/change-info")
    @ApiOperation("修改信息")
    public Result<UserInfoVo> changeInfo(@Validated @RequestBody ChangeInfoVo vo) {
        return Result.ofBody(this.userService.changeInfo(vo));
    }

    @Autowired
    @Qualifier("RedisCanvasServiceImpl")
    public void setCanvasService(ICanvasService canvasService) {
        this.canvasService = canvasService;
    }

    @Autowired
    public void setUserService(IUserService userService) {
        this.userService = userService;
    }
}
